package com.nnnu.wsnackshop.controller.admin;

import com.nnnu.wsnackshop.constant.MessageConstants;
import com.nnnu.wsnackshop.pojo.dto.*;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.IProductSkuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 商品SKU表 前端控制器
 * </p>
 *
 * @author zk
 * @since 2025-05-14
 */
@RestController("adminProductSkuController")
@RequestMapping("/api/admin/sku")
@Tag(name = "管理端-SKU接口")
@Slf4j
@RequiredArgsConstructor
public class ProductSkuController {

    private final IProductSkuService skuService;

    @Operation(summary = "分页查询SKU")
    @GetMapping("/page")
    @RequiresRoles(value = { "superAdmin", "admin" }, logical = Logical.OR)
    @RequiresPermissions("sku:read")
    public Result<?> pageSku(@ParameterObject SkuPageDTO dto) {
        return Result.success(skuService.pageSku(dto), MessageConstants.SELECT_OK);
    }

    @Operation(summary = "获取SKU详情")
    @GetMapping("/{id}")
    @RequiresPermissions("sku:read")
    public Result<?> getSku(@PathVariable Integer id) {
        return Result.success(skuService.getSkuDetail(id), MessageConstants.SELECT_OK);
    }

    @Operation(summary = "根据商品ID查询SKU列表")
    @GetMapping("/product/{productId}")
    @RequiresPermissions("sku:read")
    public Result<?> getSkuByProduct(@PathVariable Integer productId) {
        return Result.success(skuService.listSkuByProduct(productId), MessageConstants.SELECT_OK);
    }

    @Operation(summary = "根据SPU ID查询SKU列表")
    @GetMapping("/spu/{spuId}")
    @RequiresPermissions("sku:read")
    public Result<?> getSkuBySpu(@PathVariable Integer spuId) {
        return Result.success(skuService.listSkuBySpu(spuId), MessageConstants.SELECT_OK);
    }

    @Operation(summary = "新增SKU")
    @PostMapping
    @RequiresPermissions("sku:create")
    @CacheEvict(cacheNames = { "userSkuBySpu", "userSkuPage", "userSkuDetail" }, allEntries = true)
    public Result<?> addSku(@Valid @RequestBody AddSkuDTO dto) {
        skuService.addSku(dto);
        return Result.success(MessageConstants.ADD_OK);
    }

    @Operation(summary = "更新SKU")
    @PutMapping
    @RequiresPermissions("sku:update")
    @CacheEvict(cacheNames = { "userSkuBySpu", "userSkuPage", "userSkuDetail" }, allEntries = true)
    public Result<?> updateSku(@Valid @RequestBody UpdateSkuDTO dto) {
        skuService.updateSku(dto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @Operation(summary = "更新SKU库存")
    @PutMapping("/stock/{id}")
    @RequiresPermissions("sku:update")
    @CacheEvict(cacheNames = { "userSkuBySpu", "userSkuPage", "userSkuDetail" }, allEntries = true)
    public Result<?> updateSkuStock(@PathVariable Integer id, @RequestParam Integer stock) {
        skuService.updateSkuStock(id, stock);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @Operation(summary = "删除SKU")
    @DeleteMapping
    @RequiresPermissions("sku:delete")
    @CacheEvict(cacheNames = { "userSkuBySpu", "userSkuPage", "userSkuDetail" }, allEntries = true)
    public Result<Void> deleteSku(@RequestParam List<Integer> ids) {
        skuService.deleteSku(ids);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @Operation(summary = "导出SKU列表Excel")
    @GetMapping("/export")
    @RequiresPermissions("sku:export")
    public void exportSku(HttpServletResponse resp) throws IOException {
        skuService.exportSku(resp);
    }

    @Operation(summary = "下载SKU导入模板")
    @GetMapping("/template")
    @RequiresPermissions("sku:read")
    public void downloadSkuTemplate(HttpServletResponse resp) throws IOException {
        skuService.downloadSkuTemplate(resp);
    }

    @Operation(summary = "导入SKU Excel")
    @PostMapping("/import")
    @RequiresPermissions("sku:import")
    @CacheEvict(cacheNames = { "userSkuBySpu", "userSkuPage", "userSkuDetail" }, allEntries = true)
    public Result<?> importSku(@RequestParam("file") MultipartFile file) throws IOException {
        skuService.importSku(file);
        return Result.success(MessageConstants.ADD_OK);
    }
}
